#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>          /* See NOTES */
#include <sys/socket.h>
#include <string.h>
#include <sys/epoll.h>

#define BUFFER_LENGTH 128
#define CONNECTLENGTH 1024

struct conn_item
{
    int fd;
    char buffer[BUFFER_LENGTH];
    int idx; // 存储buffer的偏移量
};

struct conn_item connlist[CONNECTLENGTH] = {0};

int main(int argc, char const *argv[])
{
    int sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if(-1 == sockfd)
    {
        perror("sock:");
        exit(-1);
    }
    struct sockaddr_in server_addr;
    memset(&server_addr, 0, sizeof(server_addr));
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(4396);
    server_addr.sin_addr.s_addr = htonl(INADDR_ANY);

    int ret = bind(sockfd, (struct sockaddr*)&server_addr, sizeof(server_addr));
    if(-1 == ret)
    {
        perror("bind:");
        close(sockfd);
        exit(-1);
    }
    if(-1 == listen(sockfd, 10))
    {
        perror("listen:");
        close(sockfd);
        exit(-1);
    }

    int epfd = epoll_create(1);
    if(-1 == epfd)
    {
        perror("epoll_create:");
        close(sockfd);
        exit(-1);
    }

    struct epoll_event ev, events[CONNECTLENGTH];
    ev.data.fd = sockfd;
    ev.evnets = EPOLLIN;
    epoll_ctl(epfd, EPOLL_CTL_ADD, sockfd, &ev);
    
    while(1)
    {
        int nready = epoll_wait(epfd, events, CONNECTLENGTH, -1);
        if(-1 == nready)
        {
            perror("epoll_wait:");
            exit(-1);
        }
        for(int i = 0; i < nready; i ++)
        {
            int connfd = events[i].data.fd;
            if(sockfd == connfd)
            {
                // 有新的连接
                struct sockaddr_in client_addr;
                socklen_t len = sizeof(client_addr);
                int clientfd = accept(sockfd, (struct sockaddr*)&client_addr, &len);
                printf("connected: %d\n", clientfd);
                connlist[clientfd].fd = clientfd;
                memset(connlist[clientfd].buffer, 0, BUFFER_LENGTH);
                connlist[clientfd].idx = 0;
                ev.data.fd = clientfd;
                ev.events = EPOLLIN;
                epoll_ctl(epfd, EPOLL_CTL_ADD, clientfd, &ev);
            }
            else if (events[i].evnets & EPOLLIN)
            {
                // 有client发送消息
                char *buffer = connlist[connfd].buffer;
                int idex = connlist[connfd].idx;
                ssize_t count = recv(connfd, buffer + idx, BUFFER_LENGTH - idx, 0);
                if(0 == count)
                {
                    printf("disconnected\n");
                    epoll_ctl(epfd, EPOLL_CTL_DEL, connfd, NULL);
                    close(connfd);
                    continue;
                }
                connlist[connfd].idx += count;
                printf("clientfd: %d, count: %d, buffer: %s\n", connfd, count, buffer);
                send(connfd, buffer, count, 0);
            }
        }
    }


    close(sockfd);
    exit(0);
}
